package com.tbit.uqbike.center.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串实体类
 *
 * @author Leon
 * 2014-4-11 上午11:42:24
 */
public class StringUtil {

    /**
     * 分解字符串(整形)
     */
    public static List<Integer> getIntegerList(String param) {
        String[] params = param.split(",");
        List<Integer> resultList = new ArrayList<Integer>();
        for (String par : params) {
            resultList.add(Integer.parseInt(par));
        }

        return resultList;
    }

    /**
     * 构造字符串
     */
    public static String getListString(List<Integer> ids) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = ids.size(); i < length; i++) {
            result.append(ids.get(i));

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getArrayListString(List<String> paramList) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = paramList.size(); i < length; i++) {
            result.append("'" + paramList.get(i) + "'");

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造HTTP字符串
     */
    public static String getArrayListStringHttp(List<String> paramList) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = paramList.size(); i < length; i++) {
            result.append(paramList.get(i));

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 分解字符串（double）
     */
    public static List<Double> getDoubleList(String param) {
        String[] params = param.split(",");
        List<Double> resultList = new ArrayList<Double>();
        for (String par : params) {
            resultList.add(Double.parseDouble(par));
        }

        return resultList;
    }

    /**
     * 获取六位随机数
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String getAuthCode() {
        String str = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        @SuppressWarnings("rawtypes")
        List list = new ArrayList();
        while (sb.length() < 6) {
            int number = random.nextInt(10);
            if (sb.length() > 0 && !"0".equals(sb.substring(0, 1))) {
                int n = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (!((Object) number).equals(list.get(i))) {
                        n++;
                    }
                }
                if (n == sb.length()) {
                    sb.append(str.charAt(number));
                    list.add(number);
                }
            } else {
                if (!"0".equals(String.valueOf(str.charAt(number)))
                        && sb.length() == 0) {
                    sb.append(str.charAt(number));
                    list.add(number);
                }
            }
        }

        return sb.toString();
    }

    /**
     * 判断字符串是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * 判断是不是手机号码
     *
     * @param mobiles
     * @return
     */
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static void main(String[] args) {
        System.out.println(isMobileNO("18205486226"));
    }

}
